package org.lansir.af.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.lansir.af.R;
import org.lansir.af.dataview.DataViewActivityParticipator;
import org.lansir.af.dataview.DataViewItem;
import org.lansir.af.dataview.DataViewItemActivityParticipator;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class ScheduleActivityInfoParticipatorFragment extends CommonFragment {
	private static final String TAG = "ScheduleActivityInfoParticipatorFragment";
	private LayoutInflater mInflater;
	// private List<Participator> lParticipators = new
	// ArrayList<Participator>();

	private ActivityInfoParticipatorAdapter adapterActivityInfoParticipator;
	private ExpandableListView elvActivityInfoParticipator;
	private List<String> lParticipatorGroup = new ArrayList<String>();
	private Map<String, List<DataViewItemActivityParticipator>> mParticipators = new HashMap<String, List<DataViewItemActivityParticipator>>();
	private DataViewItemActivityParticipator longPressParticipator = null;

	private DataViewActivityParticipator dataViewActivityParticipator;
	private String[] sParticipatorGroup;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View viewRoot = inflater.inflate(R.layout.fragment_schedule_activity_info_participator, null);
		long lRowId = requestedRowId();
		dataViewActivityParticipator = new DataViewActivityParticipator(this.userdb, lRowId);

		mInflater = inflater;
		elvActivityInfoParticipator = (ExpandableListView) viewRoot.findViewById(R.id.elvActivityInfoParticipator);
		adapterActivityInfoParticipator = new ActivityInfoParticipatorAdapter();
		elvActivityInfoParticipator.setAdapter(adapterActivityInfoParticipator);
		registerForContextMenu(elvActivityInfoParticipator);
		elvActivityInfoParticipator.setDivider(null);
		initStates();
		return viewRoot;
	}

	protected void initStates() {
		sParticipatorGroup = this.getResources().getStringArray(R.array.activityInfoParticipatorAttend);
		for (String parGroup : sParticipatorGroup) {
			this.lParticipatorGroup.add(parGroup);
			mParticipators.put(parGroup, new ArrayList<DataViewItemActivityParticipator>());
		}

		dataViewActivityParticipator.reloadTable();
		Log.v(TAG, "DataViewItemActivityParticipator size: " + dataViewActivityParticipator.getRowsForTotal().size());
		for (DataViewItem dataViewItem : dataViewActivityParticipator.getRowsForTotal()) {
			DataViewItemActivityParticipator dataViewItemActivityParticipator = (DataViewItemActivityParticipator) dataViewItem;
			Log.v(TAG, "DataViewItemActivityParticipator name:" + dataViewItemActivityParticipator.getParticipatorName());
			mParticipators.get(sParticipatorGroup[dataViewItemActivityParticipator.getReply()]).add(dataViewItemActivityParticipator);
		}

		for (int i = 0; i < lParticipatorGroup.size(); i++) {
			elvActivityInfoParticipator.expandGroup(i);
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
		// menuinfo该对象提供了选中对象的附加信息

		int type = ExpandableListView.getPackedPositionType(info.packedPosition);

		int group = ExpandableListView.getPackedPositionGroup(info.packedPosition);

		int child = ExpandableListView.getPackedPositionChild(info.packedPosition);

		// 响应
		if (type == 0) {// 分组长按事件

		} else if (type == 1) {// 长按好友列表项
			DataViewItemActivityParticipator participator = mParticipators.get(lParticipatorGroup.get(group)).get(child);
			longPressParticipator = participator;
			MenuInflater inflater = this.getActivity().getMenuInflater();
			if (participator.isOther()) {
				inflater.inflate(R.menu.schedule_activity_info_participator_other, menu);
			}
			menu.setHeaderTitle(participator.getParticipatorName());
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.menuitem_activity_info_participator_normal_sms: {
			Uri smsToUri = Uri.parse("smsto:" + longPressParticipator.getParticipatorId());
			Intent intent = new Intent(android.content.Intent.ACTION_SENDTO, smsToUri);
			startActivity(intent);
		}
			break;
		case R.id.menuitem_activity_info_participator_normal_phone: {
			Uri phoneToUri = Uri.parse("tel:" + longPressParticipator.getParticipatorId());
			Intent intent = new Intent(android.content.Intent.ACTION_CALL, phoneToUri);
			startActivity(intent);
		}
			break;

		}
		return super.onContextItemSelected(item);
	}

	private class ActivityInfoParticipatorAdapter extends BaseExpandableListAdapter {

		@Override
		public Object getChild(int groupPosition, int childPosition) {

			return mParticipators.get(lParticipatorGroup.get(groupPosition)).get(childPosition);
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {

			return mParticipators.get(lParticipatorGroup.get(groupPosition)).get(childPosition).getId();
		}

		@Override
		public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				v = mInflater.inflate(R.layout.fragment_schedule_activity_info_participator_listchild, null);
			}
			DataViewItemActivityParticipator participator = mParticipators.get(lParticipatorGroup.get(groupPosition)).get(childPosition);
			bindChildView(v, participator);
			return v;
		}

		@Override
		public int getChildrenCount(int groupPosition) {

			return mParticipators.get(lParticipatorGroup.get(groupPosition)).size();
		}

		@Override
		public Object getGroup(int groupPosition) {

			return lParticipatorGroup.get(groupPosition);
		}

		@Override
		public int getGroupCount() {

			return lParticipatorGroup.size();
		}

		@Override
		public long getGroupId(int groupPosition) {

			return lParticipatorGroup.get(groupPosition).hashCode();
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				v = mInflater.inflate(R.layout.fragment_schedule_activity_info_participator_listgroup, null);
			}
			String group = lParticipatorGroup.get(groupPosition);
			bindGroupView(v, group);

			return v;
		}

		@Override
		public boolean hasStableIds() {

			return false;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {

			return true;
		}

		private void bindGroupView(View view, String curGroup) {
			if (curGroup != null) {
				TextView v = (TextView) view.findViewById(R.id.tvActivityInfoParticipatorsGroupName);
				v.setText(curGroup);
				v = (TextView) view.findViewById(R.id.tvActivityInfoParticipatorsChildNum);
				v.setText("" + mParticipators.get(curGroup).size());

			}

		}

		private void bindChildView(View view, DataViewItemActivityParticipator participator) {
			if (participator != null) {
				TextView v = (TextView) view.findViewById(R.id.tvActivityInfoParticipatorName);
				v.setText(participator.getParticipatorName());

			}
		}

	};

}
